package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cheer.entity.OaIntegralEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 积分明细
 *
 * @author cheer
 */
@Mapper
@Repository
public interface OaIntegralDao extends BaseMapper<OaIntegralEntity> {

    default List<OaIntegralEntity> selectListByOpenid(String openid) {
        return selectList(new QueryWrapper<OaIntegralEntity>()
                .eq("openid", openid)
                .orderByDesc("add_time"));
    }

    default List<OaIntegralEntity> selectListByOpenidLimit(int start, int offset, String openid) {
        return selectList(new QueryWrapper<OaIntegralEntity>()
                .eq("openid", openid)
                .orderByDesc("add_time")
                .last("limit " + start + "," + offset));
    }

    default Integer selectCountByOpenid(String openid, Integer type, Date start, Date end) {
        return selectCount(new QueryWrapper<OaIntegralEntity>()
                .eq("openid", openid)
                .eq("type", type)
                .ge("add_time", start)
                .le("add_time", end));
    }

    Integer selectSumByOpenid(@Param("openid") String openid, @Param("start") Date start, @Param("end") Date end);

    default Integer selectCountByItemsId(String openid, Integer type, Integer itemsId) {
        return selectCount(new QueryWrapper<OaIntegralEntity>()
                .eq("openid", openid)
                .eq("type", type)
                .eq("items_id", itemsId));
    }

    default Integer selectCountByOpenidAndItemsId(String openid, Integer type, Integer itemsId, Date start, Date end) {
        return selectCount(new QueryWrapper<OaIntegralEntity>()
                .eq("openid", openid)
                .eq("type", type)
                .eq("items_id", itemsId)
                .ge("add_time", start)
                .le("add_time", end));
    }

    default List<OaIntegralEntity> selectCountByToday(String openid, Date start, Date end) {
        return selectList(new QueryWrapper<OaIntegralEntity>()
                .select("integral")
                .eq("openid", openid)
                .ge("add_time", start)
                .le("add_time", end));
    }

}
